<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <script type="text/javascript" src="./functions.js"></script>
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
</head>

<body>
    <script type="text/javascript">

        var userName;
        var passWord;
        var xmlHttpRequest;

        //XmlHttpRequest对象  
        function createXmlHttpRequest() {
            if (window.ActiveXObject) { //如果是IE浏览器  
                return new ActiveXObject("Microsoft.XMLHTTP");
            } else if (window.XMLHttpRequest) { //非IE浏览器  
                return new XMLHttpRequest();
            }
        }
        // var call = {
        //     "callbackUrl":"http://api.tuntun.roc.ltd/storage/aliyun/oss/sts/upload/callback",
        //     "callbackHost":"api.tuntun.roc.ltd",
        //     "callbackBody":"filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}",
        //     "callbackBodyType":"application/x-www-form-urlencoded"
        //   }
        // var callback = Base64.encode(JSON.stringify(call));

        $(document).ready(function() {
            $.ajax({
                url: '/adm/aliyun/oss/sts/upload/token/for/admin',
                success: function (data) {
                    alert(JSON.stringify(data));
                }
            })
        });

        var POLICY_JSON = {
            "expiration": "2020-12-01T12:00:00.000Z",
            "conditions": [
                ["starts-with", "$key", ""],
                { "bucket": 'ttresource' },
                // {"callback": callback},
                ["starts-with", "$Content-Type", ""],
                ["content-length-range", 0, 524288000]
            ]
        };

        var secret = 'gA6Hn6fADTlcwpClPTEpHyJv8pVDCH';
        var policyBase64 = Base64.encode(JSON.stringify(POLICY_JSON));
        var signature = b64_hmac_sha1(secret, policyBase64);

        function uploadProgress(evt) {
            if (evt.lengthComputable) {
                var percentComplete = Math.round(evt.loaded * 100 / evt.total);
                document.getElementById('progressNumber').innerHTML = percentComplete.toString() + '%';
            }
            else {
                document.getElementById('progressNumber').innerHTML = 'unable to compute';
            }
        }
        function uploadComplete(evt) {
            var str = JSON.stringify(evt.target.responseText)
            var s1 = str.split("<Location>");
            var s2 = s1[1].split("</Location>")
            document.getElementById('should').innerHTML = s2[0];
            if(opener !== null) {
              opener.document.getElementById('demoInput').value = s2[0];
            }
            window.close()
        }
        function uploadFailed(evt) {
            alert("There was an error attempting to upload the file." + evt);
        }
        function uploadCanceled(evt) {
            alert("The upload has been canceled by the user or the browser dropped the connection.");
        }
        function uploadFile() {
            var file = document.getElementById('file').files[0];
            var fd = new FormData();
            var key = "events/" + (new Date).getTime() + '-' + file.name;
            fd.append('key', key);
            fd.append('Content-Type', file.type);
            fd.append('OSSAccessKeyId', 'LTAIKGVABvNiwyVu');
            fd.append('policy', policyBase64)
            fd.append('signature', signature);
            fd.append("file", file);
            fd.append("success_action_status", "201");
            // fd.append("Content-Disposition", "form-data");
            // fd.append("callback", callback);
            var xhr = createXmlHttpRequest()
            xhr.upload.addEventListener("progress", uploadProgress, false);
            xhr.addEventListener("load", uploadComplete, false);
            xhr.addEventListener("error", uploadFailed, false);
            xhr.addEventListener("abort", uploadCanceled, false);
            xhr.open('post', 'https://ttresource.oss-cn-beijing.aliyuncs.com', true); //MUST BE LAST LINE BEFORE YOU SEND
            xhr.send(fd);
        }
    </script>
    <form id="form1" enctype="multipart/form-data" method="post">
        <div class="row">
            <label for="file">上传视频</label>
            <br />
            <input type="file" name="file" id="file" onchange="uploadFile()" />
        </div>
        <div id="fileName"></div>
        <div id="fileSize"></div>
        <div id="fileType"></div>
        <div class="row" id="should">
            <!-- <input type="button" onclick="uploadFile()" value="Upload" /> -->
        </div>
        <div id="progressNumber"></div>

</body>

</html>